package com.cloudlibrary.service.impl;

import com.cloudlibrary.domain.Book;
import com.cloudlibrary.domain.Record;
import com.cloudlibrary.domain.User;
import com.cloudlibrary.entity.PageResult;
import com.cloudlibrary.entity.Result;
import com.cloudlibrary.mapper.BookMapper;
import com.cloudlibrary.service.BookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RecordServiceImpl recordService;

    //查询最新上架的图书
    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        /*分页*/
        PageHelper.startPage(pageNum, pageSize);
        /*获取分页对象*/
        Page<Book> page = bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id查询图书信息
    @Override
    public Book findById(String id) {
        Book book = bookMapper.findById(id);
        return book;
    }

    //借阅图书
    @Override
    public Integer borrowBook(Book book) {
        Integer integer = bookMapper.editBook(book);
        return integer;
    }

    //分页查询图书
    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        /*分页*/
        PageHelper.startPage(pageNum, pageSize);
        /*获取分页对象*/
        Page<Book> page = bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //新增图书
    @Override
    public Integer addBook(Book book) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //设置新增图书的上架时间
        book.setUploadTime(dateFormat.format(new Date()));
        return bookMapper.addBook(book);
    }

    @Override
    /* 编辑图书信息*/
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }

    //查询当前借阅的图书
    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize); // 设置分页查询的参数，开始分页
        Page<Book> page;
        book.setBorrower(user.getName()); //将当前登录的用户放入查询条件中
        //如果是管理员，查询当前用户借阅但未归还的图书和所有待确认归还的图书
        if ("ADMIN".equals(user.getRole())) {
            page = bookMapper.selectBorrowed(book);
        } else {//如果是普通用户，查询当前用户借阅但未归还的图书
            page = bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    /*归还图书*/
    @Override
    public boolean returnBook(String id, User user) {
        // 根据图书id查询出图书的完整信息
        Book book = this.findById(id);
       //Book book = null;
        // 再次核验当前登录人员和图书借阅者是不是同一个人
        boolean rb = book.getBorrower().equals(user.getName());
        // 如果是同一个人，允许归还
        if (rb) {
            // 将图书借阅状态修改为归还中
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }

    /*归还确认*/
    @Override
    public Integer returnConfirm(String id) {
        // 根据图书id查询图书的完整信息
        Book book = this.findById(id);
        String borrower = book.getBorrower();
        String borrowTime = book.getBorrowTime();
        String returnTime = book.getReturnTime();
        // 将图书的借阅状态修改为可借阅
        book.setStatus("0");
        book.setBorrower(""); // 清除当前图书的借阅人信息
        book.setBorrowTime(""); // 清除当前图书的借阅时间信息
        // 清除当前图书的预计归还时间信息
        book.setReturnTime("");
        Integer integer = bookMapper.editBook(book);
        if (integer == 1) {
            Record record = new Record();
            record.setBookname(book.getName());
            record.setBookisbn(book.getIsbn());
            record.setBorrower(borrower);
            record.setBorrowTime(borrowTime);
            record.setRemandTime(returnTime);
            recordService.addRecord(record);
        }
        return integer;
    }


}
